<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>switch 自定义开关</title>
    <style>
        body,
        html {
            background-color: #002b47;
            padding: 0;
            margin: 0;
            height: 100vh;
            width: 100%;
        }

        .box {
            width: 100%;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .container {
            width: 80px;
            height: 30px;
            border-radius: 30px;
            background-color: aliceblue;
            display: flex;
            position: relative;
            overflow: hidden;
        }

        .close {
            width: 100%;
            height: 30px;
            background-color: #fff;
            position: absolute;
            left: 0;
            top: 0;
            translate: -100% 0;
            transition: translate 0.5s;
        }

        .circle {
            width: 26px;
            height: 26px;
            background-color: #ff9b05;
            border-radius: 50%;
            position: absolute;
            z-index: 20;
            margin: 2px;
            transition: all 0.5s;
        }

        .open {
            width: 100%;
            height: 30px;
            background-color: #ff0000;
            position: absolute;
            left: 0;
            top: 0;
            translate: 0 0;
            transition: translate 0.5s;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="container">
            <!-- 关 -->
            <div class="open ">
                <div></div>
                <div></div>
                <div></div>
                <div></div>
                <div></div>
            </div>
            <!-- 按钮 -->
            <div class="circle" id="btn"></div>
            <!-- 开 -->
            <div class="close">
                <div></div>
                <div></div>
                <div></div>
                <div></div>
                <div></div>
            </div>
        </div>
    </div>

    <script>
        let btn = document.getElementById('btn')
        let container = document.querySelector('.container')
        let close = document.querySelector('.close')
        let open = document.querySelector('.open')
        let flag = false;
        // 容器宽度减去按钮宽度 减去 按钮的margin
        let width = container.clientWidth - btn.clientWidth - 4;
        btn.onclick = function () {
            if (flag) {
                // container.style.background = '#ff0000'
                
                close.style.translate = '-100% 0'
                open.style.translate = '0 0'
                flag = false;
                btn.style.translate = '0 0'

            }
            else {
                container.style.background = '#fff'
                close.style.translate = '0 0'
                open.style.translate = '100% 0'
                flag = true;
                btn.style.translate = `${width}px 0`
            }

        }
    </script>
</body>

</html>